package com.lyx.crm.controller;

import com.lyx.crm.annotation.RequiredPermission;
import com.lyx.crm.base.BaseController;
import com.lyx.crm.base.ResultInfo;
import com.lyx.crm.enums.StateStatus;
import com.lyx.crm.query.SaleChanceQuery;
import com.lyx.crm.service.SaleChanceService;
import com.lyx.crm.utils.CookieUtil;
import com.lyx.crm.utils.LoginUserUtil;
import com.lyx.crm.vo.SaleChance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Package: com.lyx.crm.controller
 * @ClassName: SaleChanceController
 * @Author: lyx
 * @CreateTime: 2021/7/20 19:43
 * @Description:
 */
@Controller
@RequestMapping("sale_chance")
public class SaleChanceController extends BaseController {
    @Autowired
    private SaleChanceService saleChanceService;

    /**
     * 分页多条件查询
     * 如果flag不为空，这查询的开发机会，如果flag为空，就是查询营销机会
     *
     * @param saleChanceQuery
     * @return
     */
    @RequiredPermission(code = "101001")
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery, Integer flag,
                                                       HttpServletRequest request) {
        if (null != flag && flag == 1) {
            //查询开发计划
            //设置分配状态
            saleChanceQuery.setState(StateStatus.STATED.getType());
            //设置指派人（当前用户id）
            int userId = LoginUserUtil.releaseUserIdFromCookie(request);
            saleChanceQuery.setAssignMan(userId);
        }


        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }

    @RequiredPermission(code = "1010")
    @RequestMapping("index")
    public String index() {
        return "saleChance/sale_chance";
    }

    /**
     * 添加营销机会
     *
     * @param saleChance
     * @param request
     * @return
     */
    @RequiredPermission(code = "101002")
    @ResponseBody
    @RequestMapping("add")
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request) {

        String userName = CookieUtil.getCookieValue(request, "userName");
        saleChance.setCreateMan(userName);
        saleChanceService.addSaleChance(saleChance);
        return success("营销机会数据添加成功");
    }

    /**
     * 更新营销机会
     *
     * @param saleChance
     * @return
     */
    @RequiredPermission(code = "101004")
    @ResponseBody
    @RequestMapping("update")
    public ResultInfo updateSaleChance(SaleChance saleChance) {

        saleChanceService.updateSaleChance(saleChance);
        return success("营销机会数据更新成功!");
    }


    /**
     * 进入添加/修改界面
     *
     * @return
     */
    @RequestMapping("toSaleChancePage")
    public String toSaleChancePage(Integer saleChanceId, HttpServletRequest request) {

        if (saleChanceId != null) {
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(saleChanceId);
            request.setAttribute("saleChance", saleChance);
        }
        return "saleChance/add_update";
    }

    @RequiredPermission(code = "101003")
    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids) {
        System.out.println(ids.length);
        saleChanceService.deleteBatch(ids);
        return success("营销机会删除成功！");
    }

    /**
     * 更新营销机会的开发状态
     *
     * @param id
     * @param devResult
     * @return
     */
    @RequestMapping("updateSaleChanceDevResult")
    @ResponseBody
    public ResultInfo updateSaleChanceDevResult(Integer id, Integer devResult) {

        saleChanceService.updateSaleChanceDevResult(id, devResult);

        return success("营销机会开发状态更新成功");
    }
}
